﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Xml.Serialization;
using System.IO;
using Microsoft.Xna.Framework;
using Zombie_Demo.Utilities;
using Zombie_Demo.User_Interface;

namespace Zombie_Demo.User_Interface
{
     [Serializable]
     [XmlRoot("Button")]
    public class Button : Element
    {
          /// <summary>
          /// DesignType sets easy colors
          /// </summary>
          protected Design.DesignType design;
         /// <summary>
         /// Stores the colors, see DesignType for what values end up on what parts of the button
         /// </summary>
          protected Color[] buttonColors = new Color[5];
         /// <summary>
         /// A Lua command to run when clicked
         /// </summary>
          protected string command;

         /// <summary>
         /// A Lua command to run when clicked
         /// </summary>
          public string Command { get { return this.command; } set { this.command = value; } }

         /// <summary>
         /// Retrieve and set button colors.
         /// </summary>
          public Color[] ButtonColors { get { return this.buttonColors; } set { this.buttonColors = value; } }

          public Button()
               : base()
          {
               SetColor();
              //Dont remember why I commented this out....
               //Rectangle r = new Rectangle(0, 0,
               //    (int)ContentLibrary.GetFont("Default").MeasureString("Most basic Button").X + 20,
               //    (int)ContentLibrary.GetFont("Default").MeasureString("Default").Y + 10);
               //BackgroundRectangle b = new BackgroundRectangle(r, buttonColors[1], this);
               //Text t = new Text("Hello :) derp", buttonColors[0], b);
               //t.Anchor = AnchorPosition.CENTER;

               //this.AddElement(b);
               //b.AddElement(t);
               //this.bounds = b.Bounds;
               if(this.Parent != null)
                    this.name = Parent.Name + "Button";
               command = "DebugPrint(\"Button " + this.name + " was clicked.\")"; 
          }

          public Button(string text, Vector2 position)
               : base()
          {
               this.Position = position;
               SetColor();
               Rectangle r = new Rectangle(0, 0,
                   (int)ContentLibrary.GetFont("Default").MeasureString(text).X + 20,
                   (int)ContentLibrary.GetFont("Default").MeasureString("Default").Y + 10);
               BackgroundRectangle b = new BackgroundRectangle(r, buttonColors[1], this);
               Text t = new Text(text, buttonColors[0], b);
               t.Anchor = AnchorPosition.CENTER;

               b.AddElement(t);
               this.bounds = b.Bounds;
               this.name = Parent.Name + "Button";
               command = "DebugPrint(\"Button " + this.name + " was clicked.\")"; 
          }

          public Button(string text, Vector2 position, Element parent)
              : base(parent)
          {
              this.Position = position;
              SetColor();
              Rectangle r = new Rectangle(0, 0,
                   (int)ContentLibrary.GetFont("Default").MeasureString(text).X + 20,
                   (int)ContentLibrary.GetFont("Default").MeasureString("Default").Y + 10);
              BackgroundRectangle b = new BackgroundRectangle(r, buttonColors[1], this);
              Text t = new Text(text, buttonColors[0], b);
              t.Anchor = AnchorPosition.CENTER;

              b.AddElement(t);
              this.bounds = b.Bounds;
              this.name = Parent.Name + "Button";
              command = "DebugPrint(\"Button " + this.name + " was clicked.\")";
          }

          public Button(string text, Vector2 position, Element parent, string name)
              : base(parent)
          {
              this.Position = position;
              SetColor();
              
              Rectangle r = new Rectangle(0, 0,
                   (int)ContentLibrary.GetFont("Default").MeasureString(text).X + 20,
                   (int)ContentLibrary.GetFont("Default").MeasureString("Default").Y + 10);
              BackgroundRectangle b = new BackgroundRectangle(r, buttonColors[1], this);
              Text t = new Text(text, buttonColors[0], b);
              t.Anchor = AnchorPosition.CENTER;

              this.bounds = b.Bounds;
              this.name = Parent.Name + name;
              command = "DebugPrint(\"Button " + this.name + " was clicked.\")";
          }

          public override void Update(GameTime gameTime)
          {
               base.Update(gameTime);
               if (this.mouseOver)
               {
                    if (Utilities.MouseWrapper.MousePressedLeft)
                    {
                        if(!Utilities.MouseWrapper.WasLMBPressed)
                            Game1.Lua.RunString(command);

                         Text t = (Text)this.Elements[0].Elements[0];
                         t.Color = this.buttonColors[4];
                         BackgroundRectangle b = (BackgroundRectangle)this.Elements[0];
                         b.Color = this.buttonColors[3];
                         
                    }
                    else
                    {
                         Text t = (Text)this.Elements[0].Elements[0];
                         t.Color = this.buttonColors[2];
                         this.Elements[0].Elements[0] = t;
                         BackgroundRectangle b = (BackgroundRectangle)this.Elements[0];
                         b.Color = this.buttonColors[1];
                         this.Elements[0] = b;
                    }
               }
               else
               {
                    Text t = (Text)this.Elements[0].Elements[0];
                    t.Color = this.buttonColors[0];
               }
          }

          private void SetColor()
          {
               for (int i = 0; i < 5; i++)
               {
                    buttonColors[i] = Design.GetDesign(this.design)[i];
               }
          }

          internal void SetCommand(string command)
          {
              this.command = command;
          }



          //public override void SetElementsToParent()
          //{
          //    base.SetElementsToParent();
          //    this.name = Parent.Name + "Button";
          //}
    }
}
